Search A 2D Matrix
August 21, 2016
Found the row the number should belong to, and then performed a binary search on it.
Full Solution in Java:
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int n = matrix[0].length-1; for(int i=0; i< matrix.length; i++){ if(matrix[i][n]==target){ return true; } else if(target< matrix[i][n]){ System.out.println(matrix[i][n]); return binarySearch(matrix[i], target); } } return false; } static boolean binarySearch(int[] arr, int target){ int low = 0; int high = arr.length-1; int mid = (low+high)/2; while(low<=high){ if(arr[mid]==target){ return true; } if(arr[mid]< target){ low = mid+1; } else{ high = mid-1; } mid = (high+low)/2; } return false; } }